package com.ebt.m.utils;

import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;
import android.text.TextUtils;

import com.ebt.m.AppContext;
import com.ebt.m.commons.utils.Logger;

import java.io.File;
import java.util.List;

/**
 * Created by wanglu on 2018/2/23.
 */

public class UriUtils {

    public static Uri getUriFromPath(String path) {
        return getUriFromFile(new File(path));
    }

    public static Uri getUriFromFile(File file) {
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        int sdkVersion = Build.VERSION.SDK_INT;
        Uri uri = Uri.fromFile(file);
        if (sdkVersion >= 24) {
            if (ContentResolver.SCHEME_FILE.equals(uri.getScheme())) {
                return FileProvider.getUriForFile(AppContext.getInstance(), ConfigData.FILE_PROVIDER, file);
            }
        }
        return uri;
    }

    public static void grantUriPermisson(Intent intent, Context context, Uri uri) {
        List<ResolveInfo> resolveInfoList = context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
        for (ResolveInfo resolveInfo : resolveInfoList) {
            String packageName = resolveInfo.activityInfo.packageName;
            context.grantUriPermission(packageName, uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
        }
    }

    public static String getPathFromUri(Context context, Uri uri) {
        String picturePath = null;
        String scheme = uri.getScheme();
        if (ContentResolver.SCHEME_CONTENT.equals(scheme)) {
            String[] filePathColumn = {MediaStore.Images.Media.DATA};
            Cursor cursor = context.getContentResolver().query(uri,
                    filePathColumn, null, null, null);//从系统表中查询指定Uri对应的照片
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            if (columnIndex >= 0) {
                picturePath = cursor.getString(columnIndex);  //获取照片路径
            } else if (TextUtils.equals(uri.getAuthority(), ConfigData.FILE_PROVIDER)) {
                picturePath = parseOwnUri(context, uri);
            }
            cursor.close();
        } else if (ContentResolver.SCHEME_FILE.equals(scheme)) {
            picturePath = uri.getPath();
        }
        Logger.e("图片路径："+picturePath);
        return picturePath;
    }

    public static String parseOwnUri(Context context, Uri uri) {
        if (uri == null) {
            return null;
        }
        String path;
        if (TextUtils.equals(uri.getAuthority(), ConfigData.FILE_PROVIDER)) {
            path = new File(uri.getPath().replace("camera_photos/", "")).getAbsolutePath();
        } else {
            path = uri.getPath();
        }
        return path;
    }
}
